<?php
class mxwView
{
    /**
     * json
     *
     * This function takes a 'View' and gets the
     * HTML content (with placeholders) into a local
     * variable via the getTemplateForView() method. 
     * The Switch/Case allows the function to apply
     * specific actions to a particular view before it 
     * is passed to parseLexicon() for localization.
     *
     * Returns a JSON Object which includes a
     * toolbar view and a content view for the
     * different views in the application.
     *
     * @param string $aView 
     * @return string (JSON Object)
     * @author Scotty Delicious
     */
    public static function json( $aView )
    {   
        switch ( $aView )
        {
            case 'overview':
                $view = self::getTemplateForView('overview');
                $view->content = self::loadProductTable($view->content);
                break;
            
            case 'new_product':
                $view = self::getTemplateForView('product');
                break;

            case 'edit_product':
                $view = self::getTemplateForView('product');
                $view->content = self::loadFormForEditing($view->content);
                break;
            
            default:
                $view = self::getTemplateForView($aView);
                break;
        }
        
        $view->toolbar = self::parseLexicon($view->toolbar);
        $view->content = self::parseLexicon($view->content);
        return json_encode($view);
    }
    
    /**
     * getTemplateForView
     *
     * @param string $aView 
     * @return object $view
     * @author Scotty Delicious
     */
    public static function getTemplateForView( $aView )
    {
        $toolbarTemplate = file_get_contents(MXW_BASE.'view/'.$aView.'/toolbar.html');
        $contentTemplate = file_get_contents(MXW_BASE.'view/'.$aView.'/'.$aView.'.html');
        $view = new stdClass;
        $view->toolbar = $toolbarTemplate;
        $view->content = $contentTemplate;
        return $view;
    }
    
    /**
     * parseLexicon
     *
     * Takes an HTML template as a string,
     * parses the string for MODx style placeholders,
     * and replaces them with their lexicon value.
     *
     * @param string $template 
     * @return string $template
     * @author Scotty Delicious
     */
    public static function parseLexicon( $template )
    {
        global $lexicon;
        $ph = '#\[\+(.*?)\+\]#se'; // RegEx for "[+" and "+]".
        $matches = array();
        preg_match_all($ph, $template, $matches);
        
        // Find each [+language_string+] placeholder and replace with $lexicon->language_string value.
        foreach ($matches[0] as $index => $match)
        {
            $string = $lexicon->$matches[1][$index];
            $template = str_replace($match, $string, $template);
        }
        return $template;
    }
    
    /**
     * loadFormForEditing
     *
     * Takes the product attributes form and preloads
     * the data for the product specified in 
     * $_POST['product_id']
     *
     * @param string $template 
     * @return string $template
     * @author Scotty Delicious
     */
    public static function loadFormForEditing( $template )
    {
        include_once(MXW_BASE.'model/Product.php');
        $product = new Product;
        $product = $product->find($_POST['product_id']);
        $template = str_replace('[+mxw_product_header+]', $product->name, $template);
        $template = str_replace('[+mxw_product_id+]', $product->id, $template);
        $template = str_replace('[+mxw_product_name+]', $product->name, $template);
        $template = str_replace('[+mxw_product_sku+]', $product->sku, $template);
        $template = str_replace('[+mxw_product_price+]', $product->price, $template);
        $template = str_replace('[+mxw_product_inventory+]', $product->inventory, $template);
        $template = str_replace('[+mxw_product_desc+]', $product->description, $template);
        return $template;
    }
    
    /**
     * loadProductTable
     *
     * Fills the products table on the overview page
     * with each products global attributes.
     * 
     * @param string $template 
     * @return void
     * @author Scotty Delicious
     */
    public static function loadProductTable( $template )
    {
        include_once(MXW_BASE.'model/Product.php');
        $tbody = '';
        $products = new Product;
        $products = $products->find('all');
        //sleep(1);
        foreach ($products as $p)
        {
            $tbody .= '<tr rel="'.$p->id.'" class="product">';
            $tbody .= '<td class="productID">'.$p->id.'</td>';
            $tbody .= '<td class="productName">'.$p->name.' <abbr title="[+mxw_variations_abbr+]">';
            $tbody .= '<span class="variations">'.$p->variations.'</span></abbr></td>';
            $tbody .= '<td>'.$p->sku.'</td>';
            $tbody .= '<td>'.$p->price.'</td>';
            $tbody .= '<td>'.$p->inventory.'</td>';
            $tbody .= '<td>'.$p->created_on.'</td>';
            $tbody .= '</tr>';
        }
        
        $template = str_replace('[+mxw_products_table_body+]', $tbody, $template);
        return $template;
    }
    
}